#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct TreeNode 
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
 
class Solution
{
public:
    vector<int> largestValues(TreeNode* root)
    {
        vector<int> ret;
        queue<TreeNode*> q;
        if (root == nullptr) return ret;
        q.push(root);
        while (q.size())
        {
            int max = INT_MIN;
            int sz = q.size();
            for (int i = 0; i < sz; i++)
            {
                TreeNode* tmp = q.front();
                q.pop();
                if (max < tmp->val) max = tmp->val;
                if (tmp->left) q.push(tmp->left);
                if (tmp->right) q.push(tmp->right);
            }
            ret.push_back(max);
        }
        return ret;
    }
};